package com.jd.jstable.tools;


import org.I0Itec.zkclient.exception.ZkMarshallingError;
import org.I0Itec.zkclient.serialize.ZkSerializer;

import java.io.UnsupportedEncodingException;

/**
 * User: wuxininfo
 */
public class StringSerializer implements ZkSerializer {
    private static final String UTF8 = "UTF-8";

    @Override
    public byte[] serialize(Object data) throws ZkMarshallingError {
        try {
            return ((String) data).getBytes(UTF8);
        } catch (UnsupportedEncodingException e) {
            throw new ZkMarshallingError(e);
        }
    }

    @Override
    public Object deserialize(byte[] bytes) throws ZkMarshallingError {
        try {
            return new String(bytes, UTF8);
        } catch (final UnsupportedEncodingException e) {
            throw new ZkMarshallingError(e);
        }
    }
}
